package org.example.jdbc;

import java.security.spec.RSAOtherPrimeInfo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class HomeWork01 {
    public static void main(String[] args) throws Exception {
        //连接数据库向库内写入数据
        //1.加载数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.构建数据库的连接对象
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        //3.创建sql语句的执行对象
        Statement statement = connection.createStatement();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.print("请输入密码：");
        String password=sc.nextLine();
        System.out.println("请输入昵称");
        String nickname=sc.nextLine();
        System.out.println("请输入年龄");
        int age=sc.nextInt();
        //4.执行sql语句  写executeUpdate 查executeQuery
        statement.executeUpdate("INSERT INTO user(username, password,nickname,age)VALUES('" + username + "','" + password + "','" + nickname + "'," + age + ")");
        System.out.println("写入成功");

    }
}
